﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Medicalsystem.MCP.FormDesigner.Property;

namespace Medicalsystem.MCP.FormDesigner.MyControls
{
    public partial class Form_MyDataGridColumnsSet : BaseForm
    {
        public Form_MyDataGridColumnsSet()
        {
            InitializeComponent();
        }

        public List<MyDataGridColumn> MyColumns
        {
            get
            {
                List<MyDataGridColumn> myColumns = new List<MyDataGridColumn>();
                for (int i = 0; i < listBox_Columns.Items.Count; i++)
                {
                    MyDataGridColumnProperty property = (MyDataGridColumnProperty)listBox_Columns.Items[i];
                    myColumns.Add(property.Control);
                }

                return myColumns;
            }
            set
            {
                if (value != null)
                {
                    foreach (MyDataGridColumn col in value)
                    {
                        MyDataGridColumnProperty property = new MyDataGridColumnProperty(col);
                        listBox_Columns.Items.Add(property);
                    }
                }
            }
        }

        private void Form_MyDataGridColumnsSet_Load(object sender, EventArgs e)
        {
            if (listBox_Columns.Items.Count > 0)
            {
                listBox_Columns.SelectedIndex = 0;
            }
        }

        #region 按钮功能

        private void button_Add_Click(object sender, EventArgs e)
        {
            MyDataGridColumn col = new MyDataGridColumn();
            col.Width = 100;
            col.HeaderText = "column";
            MyDataGridColumnProperty property = new MyDataGridColumnProperty(col);
            listBox_Columns.Items.Add(property);
            listBox_Columns.SelectedIndex = listBox_Columns.Items.Count - 1;
        }

        private void button_Remove_Click(object sender, EventArgs e)
        {
            if (listBox_Columns.SelectedItem != null)
            {
                int index = listBox_Columns.SelectedIndex;
                listBox_Columns.Items.Remove(listBox_Columns.SelectedItem);
                if (listBox_Columns.Items.Count > 0)
                {
                    if (index < listBox_Columns.Items.Count)
                    {
                        listBox_Columns.SelectedIndex = index;
                    }
                    else
                    {
                        listBox_Columns.SelectedIndex = index - 1;
                    }
                }
            }
        }

        private void button_OK_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
        }

        private void button_Cancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        private void button_Up_Click(object sender, EventArgs e)
        {
        }

        private void button_Down_Click(object sender, EventArgs e)
        {
        }

        #endregion 按钮功能

        private void listBox_Columns_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.propertyGrid1.SelectedObject = listBox_Columns.SelectedItem;
        }
    }
}